<?php

namespace app\common\logic\advert;

use app\common\model\advert\Advert;
use app\common\model\advert\AdvertLink as AdvertLinkModel;

/**
 * 附件管理
 */
class AdvertLink extends AdvertLinkModel
{

    public function getLinkByKey($key)
    {
        $advert_id = app(Advert::class)->alias('a')->join('advert_place p', "p.key='{$key}' and p.id=a.place")->value('a.id');

        $data      = app(AdvertLink::class)->where('advert_id', $advert_id)->select();

        return $data;
    }

    public function getLinkUrlByKey($key)
    {
        $image_url = [];
        $data      = $this->getLinkByKey($key)->toArray();
        foreach ($data as $v) {
            $image_url[] = $v['image_url'];
        }

        return $image_url;
    }

    public function getLinkByKeyAndMhmId($key, $mhm_id = 1)
    {
        $advert_id = app(Advert::class)
            ->withoutGlobalScope(['mhmId'])
            ->alias('a')
            ->join('advert_place p', "p.key='{$key}' and p.id=a.place AND a.mhm_id=" . $mhm_id)
            ->value('a.id');

        return app(AdvertLink::class)->where('advert_id', $advert_id)->select();
    }
}
